package com.zbkj.service.service.impl.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.common.model.shop.EbShopTypeCommission;
import com.zbkj.service.dao.shop.EbShopTypeCommissionDao;
import com.zbkj.service.service.shop.EbShopTypeCommissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EbShopTypeCommissionServiceImpl implements EbShopTypeCommissionService {

    @Autowired
    EbShopTypeCommissionDao ebShopTypeCommissionDao;

    @Override
    public int add(List<EbShopTypeCommission> ebShopTypeCommissionList) {
        Date date = new Date();
        for (EbShopTypeCommission ebShopTypeCommission : ebShopTypeCommissionList) {
            ebShopTypeCommission.setCreateTime(date);
            ebShopTypeCommission.setUpdateTime(date);
            ebShopTypeCommissionDao.insert(ebShopTypeCommission);
        }
        return ebShopTypeCommissionList.size();
    }

    @Override
    public int update(List<EbShopTypeCommission> ebShopTypeCommissionList) {
        ebShopTypeCommissionDao.delete(new LambdaQueryWrapper<EbShopTypeCommission>()
                .eq(EbShopTypeCommission::getShopTypeId, ebShopTypeCommissionList.get(0).getShopTypeId())
        );
        return add(ebShopTypeCommissionList);
    }

    @Override
    public EbShopTypeCommission findById(Long id) {
        return ebShopTypeCommissionDao.selectById(id);
    }

    @Override
    public List<EbShopTypeCommission> findByShopTypeId(Integer shopTypeId) {

        return ebShopTypeCommissionDao.selectList(new LambdaQueryWrapper<EbShopTypeCommission>()
                .eq(EbShopTypeCommission::getShopTypeId, shopTypeId).orderByDesc(EbShopTypeCommission::getId)
        );
    }

    @Override
    public EbShopTypeCommission findByProductClassificationId(Integer productClassificationId, Integer shopTypeId) {

        return ebShopTypeCommissionDao.selectOne(new LambdaQueryWrapper<EbShopTypeCommission>()
                .eq(EbShopTypeCommission::getProductClassificationId, productClassificationId)
                .eq(EbShopTypeCommission::getShopTypeId, shopTypeId)
                .last("limit 1")
        );
    }
}
